package si.unimb.src.wifisms.entity;

import java.io.Serializable;

/**
 * 
 * @author Ales
 * 
 * Nosilec podatkov za uporabniski racun.
 * Implementiran vmesnik Serializable (zaradi serializacije na disk).
 *
 */
public class Account implements Serializable {

	private static final long serialVersionUID = 2487222520727277691L;

	// Kratek opis
	private String description;
	
	// Prijavni podatki
	private String username;
	private String password = null; // ni nujno izpolnjeno
	
	// Ali naj shranim geslo?
	private boolean rememberPassword = true;
	
	// Ali naj shranjuje poslana sporocila v telefon
	private boolean saveMessages = false;
	
	// AuthToken
	private AuthToken authToken = null;
	
	/**
	 * 
	 * Privzeti konstruktor
	 * 
	 */
	public Account() {}
	
	/**
	 * 
	 * Generiranje AuthTokena za trenutni Account.
	 * 
	 */
	public AuthToken getAuthToken() throws InvalidTokenException {
	
		if(authToken == null) {
		
			authToken = new AuthToken(this);
			
		}
		
		return authToken;
		
	}
	
	/**
	 * 
	 * Razveljavimo AuthToken za trenutni Account.
	 * 
	 */
	public void invalidateToken() {

		authToken = null;
	
	}
	
	/* GETTERS / SETTERS */
	public String getDescription() {
		return description;
	}
	
	public void setDescription(String description) {
		this.description = description;
	}
	
	public String getUsername() {
		return username;
	}
	
	public void setUsername(String username) {
		
		if(username == null || !username.equals(this.username)) {
			
			invalidateToken();
			
		}
		
		this.username = username;
		
	}
	
	public String getPassword() {
		return password;
	}
	
	public void setPassword(String password) {

		if(password == null || !password.equals(this.password)) {
			
			invalidateToken();
			
		}
		
		this.password = password;
	}

	public boolean getSaveMessages() {
		return saveMessages;
	}

	public void setSaveMessages(boolean saveMessages) {
		this.saveMessages = saveMessages;
	}

	public void setRememberPassword(boolean rememberPassword) {
		this.rememberPassword = rememberPassword;
	}

	public boolean getRememberPassword() {
		return rememberPassword;
	}
	
	@Override
	public String toString() {
		return description + " [" + username + "]";
	}
	
}
